Script to copy keyvault secrets between keyvaults

Tue, October 1, 2024 - 1 min read
copy_secrets.ps1
 
 
$sourceVaultName = "keyvaultName"
$destVaultName = "keyvaultName"
 
Connect-AzAccount
$sourceSub = 'subScriptionId'
$destSub = 'subScriptionId'
Set-AzContext -Subscription $sourceSub
$secretNames = (Get-AzKeyVaultSecret -VaultName $sourceVaultName).Name
 
Set-AzContext -Subscription $destSub
$secretNames.foreach{
    Set-AzKeyVaultSecret -VaultName $destVaultName -Name $_ `
        -SecretValue (Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $_).SecretValue
}